$(function() {
 // Handler for .ready() called.
        console.log('ready');
	//Get the currently logged in username
	$.get('/user', function(user) {
  		console.log("current user is "+user);
	});

	//Bind the simple post test button
        $('#simple_post_button').bind('click', function() {
                console.log("Simple Post Button");
                $.ajax({
                        url: "api/dataTask",
                        dataType: "json",
                async: false,
                        data: {'name': 'Test Group', 'description': 'Testing from the application', 'email': 'fake@fakeDomain.com'},
                        type: 'POST',
		  success: function(data){
			console.log("POST TEST SUCCESS");
			$('#PostResult').html(data);
			console.log("data: " + data);
		  },

                error: ajaxError
                });
        });

});

function simpleIndex(){
	$.ajax({
		url: "api/simple",
		context: document.body,
		success: function(data){
			console.log("doing simpleIndex()");
			$('#IndexResult').html(data);
		}
	});
}

function simpleTest(){
	alert("TEST");
	$.ajax({
		url: "api/dataTask",
		context: document.body,
		success: function(data){
			$('#IndexResult').html(data);
		}
	});
}

function simplePostTest(){

	//alert("POST TEST");
	console.log("POST TEST");

	$.ajax({
		url: "api/dataTask/group",
		data: {'name': 'Test Group', 'description': 'Testing from the application', 'email': 'fake@fakeDomain.com'},
		context: document.body,
		dataType: "json",
		type: 'POST',
		success: function(data){
			console.log("POST TEST SUCCESS");
			$('#PostResult').html(data);
			console.log("data: " + data);
		},
		error: ajaxError
	});

	console.log("POST TEST DONE");

}

function simpleGetTest(){

	console.log("GET TEST");
	$.ajax({
		url: "api/simple/testItemValue",
		context: document.body,
		success: function(data){
			console.log("GET TEST SUCCESS");
			$('#GetResult').html(data);
		}
	});
	console.log("GET TEST DONE");
}


function simpleIndex(){
	$.ajax({
		url: "api/simple",
		context: document.body,
		success: function(data){
			$('#IndexResult').html(data);
		}
	});
}
function simpleGet(){
	$.ajax({
		url: "api/simple/testItemValue",
		context: document.body,
		success: function(data){
			$('#GetResult').html(data);
		}
	});
}
function simplePost(){
	console.log("Entering simplePost.");
	$.ajax({
		url: "api/simple",
		data: {'itemValue': 'testItemValue'},
		context: document.body,
		type: 'POST',
		success: function(data){
			console.log("DOING SIMPLE POST");
			$('#PostResult').html(data);
			console.log("DATA: "+data);
		}
	});
}
function simplePut(){
	$.ajax({
		url: "api/simple/testItemValue",
		context: document.body,
		data: {'itemValue': 'testItemNewValue'},
		headers: {'X-HTTP-Method-Override': 'PUT'},
		type: 'POST',
		success: function(data){
			$('#PutResult').html(data);
		}
	});
}
function simpleDelete(){
	$.ajax({
		url: "api/simple/testItem",
		context: document.body,
		type: 'DELETE',
		success: function(data){
			$('#DeleteResult').html(data);
		}
	});
}
function ajaxError(jqXHR, textStatus, errorThrown){
        console.log('ajaxError '+textStatus+' '+errorThrown);
        $('#error_message').remove();
        $("#error_message_template").tmpl( {errorName: textStatus, errorDescription: errorThrown} ).appendTo( "#error_dialog_content" );
        $.mobile.changePage($('#error_dialog'), {
                transition: "pop",
                reverse: false,
                changeHash: false
        });
}